<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace longbingcore\wxcore;

class Moor
{
    protected $accountid;
    protected $url;
    protected $secret;
    protected $reminder_text;
    protected $help_tmpl_text;
    protected $reminder_phone;
    protected $order_end_tmpl_text;
    protected static $uniacid;
    public function __construct($uniacid)
    {
        self::$uniacid = $uniacid;
        $config_model = new \app\virtual\model\Config();
        $config = $config_model->dataInfo(["uniacid" => $uniacid]);
        $this->accountid = $config["moor_id"];
        $this->url = $config["moor_url"];
        $this->secret = $config["moor_secret"];
        $this->reminder_text = $config["reminder_text"];
        $this->help_tmpl_text = $config["help_tmpl_text"];
        $this->order_end_tmpl_text = $config["order_end_tmpl_text"];
        $this->reminder_phone = $config["moor_reminder_phone"];
    }
    public function bindphone($phoneA, $phoneB, $phoneX, $order_id)
    {
        $time = date("YmdHis");
        $authorization = base64_encode($this->accountid . ":" . $time);
        $sig = strtoupper(md5($this->accountid . $this->secret . $time));
        $data = ["caller" => $phoneA, "called" => $phoneB, "midNum" => $phoneX, "needRecord" => "true", "userData" => $order_id, "expiration" => 30];
        $url = $this->url . "/v20160818/rlxh/midNumBindForAXB/" . $this->accountid . "?sig=" . $sig;
        $res = $this->curlPost($url, $data, $authorization);
        $res = !empty($res) ? json_decode($res, true) : [];
        return $res;
    }
    public function webCallPhone($phoneA, $phoneB, $phoneX, $order_code)
    {
        $time = date("YmdHis");
        $authorization = base64_encode($this->accountid . ":" . $time);
        $sig = strtoupper(md5($this->accountid . $this->secret . $time));
        $data = ["Action" => "Webcall", "ServiceNo" => $phoneX, "Exten" => $phoneA, "WebCallType" => "asynchronous", "Variable" => "phoneNum:" . $phoneB, "CallBackType" => "post", "ActionID" => $order_code, "CallBackUrl" => "https://" . $_SERVER["SERVER_NAME"] . "/virtual/CallBack/aliyunCallBackMoor"];
        $url = $this->url . "/v20160818/webCall/webCall/" . $this->accountid . "?sig=" . $sig;
        $res = $this->curlPost($url, $data, $authorization);
        $res = !empty($res) ? json_decode($res, true) : [];
        return $res;
    }
    public function delBind($mappingId, $midNum)
    {
        $time = date("YmdHis");
        $authorization = base64_encode($this->accountid . ":" . $time);
        $sig = strtoupper(md5($this->accountid . $this->secret . $time));
        $url = $this->url . "/v20160818/rlxh/midNumUnBindForAXB/" . $this->accountid . "?sig=" . $sig;
        $data = ["mappingId" => $mappingId, "midNum" => $midNum];
        $res = $this->curlPost($url, $data, $authorization);
        $res = !empty($res) ? json_decode($res, true) : [];
        return $res;
    }
    public function webCall($phone, $type = 1)
    {
        $time = date("YmdHis");
        $authorization = base64_encode($this->accountid . ":" . $time);
        $sig = strtoupper(md5($this->accountid . $this->secret . $time));
        if($type == 1) {
            $text = $this->reminder_text;
        } elseif($type == 2) {
            $text = $this->help_tmpl_text;
        } else {
            $text = $this->order_end_tmpl_text;
        }
        $data = ["Action" => "Webcall", "ServiceNo" => $this->reminder_phone, "Exten" => $phone, "Variable" => "text:" . $text, "WebCallType" => "asynchronous", "Timeout" => 60, "CallBackUrl" => $this->url];
        $url = $this->url . "/v20160818/webCall/webCall/" . $this->accountid . "?sig=" . $sig;
        $res = $this->curlPost($url, $data, $authorization);
        $res = !empty($res) ? json_decode($res, true) : [];
        return $res;
    }
    public function getShortTmp()
    {
        $time = date("YmdHis");
        $authorization = base64_encode($this->accountid . ":" . $time);
        $sig = strtoupper(md5($this->accountid . $this->secret . $time));
        $url = $this->url . "/v20160818/sms/getSmsTemplate/" . $this->accountid . "?sig=" . $sig;
        $res = $this->curlPost($url, [], $authorization);
        $res = !empty($res) ? json_decode($res, true) : [];
        return $res;
    }
    public function sendShortMsg($phone, $data, $tmpl)
    {
        $time = date("YmdHis");
        $authorization = base64_encode($this->accountid . ":" . $time);
        $sig = strtoupper(md5($this->accountid . $this->secret . $time));
        $data["num"] = $phone;
        $data["templateNum"] = $tmpl;
        $url = $this->url . "/v20160818/sms/sendInterfaceTemplateSms/" . $this->accountid . "?sig=" . $sig;
        $res = $this->curlPost($url, $data, $authorization);
        $res = !empty($res) ? json_decode($res, true) : [];
        $order_model = new \app\massage\model\Order();
        $order_model->dataUpdate(["id" => 8], ["text" => serialize($data)]);
        $order_model->dataUpdate(["id" => 9], ["text" => serialize($res)]);
        if(!empty($res["code"])) {
            $res["Message"] = $res["message"];
            return $res;
        }
        if(isset($res["success"]) && $res["success"]) {
            $res["Message"] = "OK";
        } else {
            $res["Message"] = $res["message"];
        }
        return $res;
    }
    public function curlPost($url, $data, $authorization)
    {
        $header[] = "Accept: application/json";
        $header[] = "Content-type: application/json;charset='utf-8'";
        $header[] = "Content-Length: " . strlen(json_encode($data));
        $header[] = "Authorization: " . $authorization;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $return = curl_exec($ch);
        if($return === false) {
            echo "CURL Error:" . curl_error($ch);
            exit;
        }
        curl_close($ch);
        return $return;
    }
}

?>